// Get IP Address
// Version 1.0
// Date 31/08/2016
// By Ben a.k.a DreamVB

#pragma comment(lib, "urlmon.lib")

#include <iostream>
#include <Windows.h>

using namespace std;
using std::cout;
using std::endl;

int main(int argc, char *argv[]){
	
	string sUrl = "https://api.ipify.org/";
	string sFile = "getip.txt";
	char sLine[80];
	FILE *fp = NULL;
	HRESULT ret;
	char *errmsg = "Cannot fetch ip address.";

	cout << "Getting ip address please wait..." << endl << endl;

	//Get the ip page and save the data to sFile
	ret =	URLDownloadToFileA(NULL, sUrl.c_str(),sFile.c_str(), 0, NULL);

	if (ret != 0){
		cout << errmsg << endl;
		exit;
	}

	//Try and open the filename downloaded.
	fp = fopen(sFile.c_str(), "r");

	if (!fp){
		cout << errmsg << endl;
		exit(1);
	}
	else{
		//Read in the line.
		fgets(sLine, 80, fp);
	}

	//Print out the ip address.
	if (strlen(sLine) > 0){
		cout << "Your ip is: " << sLine << endl;
	}

	//Close file
	fclose(fp);
	//Keep console open.
	system("pause");

	return 0;
}